package com.dmwork.service.interaction.util;

import com.google.gson.Gson;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MQHelper {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 向service-work-center发送修改指定作品收藏数的请求
     * @param workId 作品id
     * @param flag "1"是增加收藏数，"0"是减少收藏数【务必为"0"或"1"】
     */
    public void modifyWorkCollectCount(String workId, String flag) {
        Map<String, String> msgMap = new HashMap<>();
        msgMap.put("workId", workId);
        msgMap.put("flag", flag); //"1"是增加收藏数，"0"是减少收藏数
        String routingKey="collect.modify."+(flag.equals("1") ? "add" : "sub");
        rabbitTemplate.convertAndSend("dmwork.collect", routingKey, new Gson().toJson(msgMap));
    }

    /**
     *  向service-work-center发送批量修改多个作品收藏数的请求
     * @param workIdList 作品id列表
     * @param flag "1"是增加收藏数，"0"是减少收藏数【务必为"0"或"1"】
     */
    public void modifyBatchWorkCollectCount(List<String> workIdList, String flag) {
        Map<String, Object> msgMap=new HashMap<>();
        msgMap.put("workIdList", workIdList);
        msgMap.put("flag", flag);
        String routingKey="collect.modify.batch."+(flag.equals("1") ? "add" : "sub");
        rabbitTemplate.convertAndSend("dmwork.collect", routingKey, msgMap);
    }
}
